import { reqAddressInfo, reqShopInfo, reqSubmitOrder } from "前台项目/api";

const state = {
    // 用户地址信息
    address: [],
    // 商品清单数据
    tradeInfo: {},
    payId:'',
};
const mutations = {
    GETADDRESS(state, address) {
        state.address = address;
    },

    GETSHOPINFO(state, tradeInfo) {
        state.tradeInfo = tradeInfo;
    },

    SUBMITINFO(state, payId){
        state.payId = payId;
    }

};
const actions = {
    // 获取用户地址信息
    async getAddress({ commit, dispatch, state }) {
        let result = await reqAddressInfo();

        if (result.code == 200) {
            // console.log(result.data)

            commit('GETADDRESS', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error(result.error))
        }

    },

    // 获取商品清单
    async getShopInfo({ commit, dispatch, state }) {
        let result = await reqShopInfo();

        if (result.code == 200) {
            commit('GETSHOPINFO', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error(result.error))
        }
    },

    // 提交订单    tradeNo 交易编码  data:付款人信息
    async submitInfo({ commit, dispatch, state }, { tradeNo, data }) {
        //提交订单的时候：返回一个很重要数据->订单ID【这笔交易唯一标识符:付款人、收款人】
        let result = await reqSubmitOrder(tradeNo, data)

        if(result.code == 200){
            commit('SUBMITINFO',result.data)
            return "ok"
        }else{
            return Promise.reject(new Error(result.message))
        }

    }

};
const getters = {

};

export default {
    state,
    mutations,
    actions,
    getters
}